package com.sunnada.edu.business.group.service;

import java.util.HashMap;
import java.util.List;
import java.util.UUID;

import com.sunnada.edu.system.pub.common.AppContext;
import com.sunnada.edu.system.pub.dao.PubDao;
import com.sunnada.edu.system.pub.util.Utils;

public class GroupMethods {

	public static int workerAdd2Group(HashMap<String, String> params) {
		PubDao dao = AppContext.getInstance().getPubDao();
		String groupId = params.get("groupId");
		String workerIds = params.get("workerIds");
		String sql = "update t_worker set  groupid='" + groupId + "' where rec_id in(" + Utils.getSqlSplitStr(workerIds) + ")";
		return dao.update(sql);

	}

	public static int getGroupByName(HashMap<String, String> params) {
		PubDao dao = AppContext.getInstance().getPubDao();
		String name = params.get("name");
		String sql = "SELECT NAME FROM T_WORKER_GROUP T WHERE  NAME='" + name + "'AND REC_STATUS!='2' ";
		return dao.selectListCot(sql);
	}

	public static int workerRemoveFromGroup(HashMap<String, String> params) {
		PubDao dao = AppContext.getInstance().getPubDao();
		String groupId = params.get("groupId");
		String workerId = params.get("workerId");
		String sql = "update t_worker set  groupid='' where groupid ='" + groupId + "'";
		return dao.update(sql);

	}

	public static int delProjectFromGroup(HashMap<String, String> params) {
		PubDao dao = AppContext.getInstance().getPubDao();
		String groupId = params.get("groupId");
		String projectId = params.get("projectId");
		String sql = "delete from t_worker_group_project where groupid ='" + groupId + "' and projectId='" + projectId + "'";
		return dao.delete(sql);

	}

	public static void projectAdd2Group(HashMap<String, String> params) {
		PubDao dao = AppContext.getInstance().getPubDao();

		String groupId = params.get("groupId");// 班组ID
		String projectIds = params.get("projectIds");
		if (Utils.isNotNull(projectIds)) {
			String[] projects = projectIds.split(",");
			for (int i = 0; i < projects.length; i++) {
				String projectId = projects[i];
				String recId = UUID.randomUUID().toString();
				// 判断班组是否已经关联项目
				if (getCotByPidAndGid(groupId, projectId) == 0) {
					String sql = "insert into t_worker_group_project(rec_id,groupId,projectId) values('" + recId + "','" + groupId + "','" + projectId + "')";
					dao.insert(sql);
				}
			}
		}

	}

	public static List getGroupPorjectList(HashMap<String, String> params) {
		PubDao dao = AppContext.getInstance().getPubDao();
		String recId = params.get("groupId");
		String sql = " select distinct  t.groupid, t.projectid,p.name projectname from t_worker_group_project t "
				+ " left join  t_project p on t.projectid = p.rec_id and p.rec_status!='2' " + " where t.groupid='" + recId + "'";
		return dao.selectList(sql);
	}

	public static int getCotByPidAndGid(String groupId, String projectId) {
		PubDao dao = AppContext.getInstance().getPubDao();
		String sql = "select rec_id from t_worker_group_project t where  groupid='" + groupId + "' and projectid='" + projectId + "' ";
		return dao.selectListCot(sql);
	}

	public static void delRelationFromWorker(HashMap<String, String> params) {
		PubDao dao = AppContext.getInstance().getPubDao();
		// String groupId = params.get("groupId");
		String groupId = params.get("recId");
		String sql = "update t_worker set  groupid='' where groupid ='" + groupId + "'";
		dao.update(sql);
	}

	public static void setMonitor(HashMap<String, String> params) {
		PubDao dao = AppContext.getInstance().getPubDao();
		int a1 = 0;
		int a2 = 0;
		String groupId = params.get("recId");// 班组ID
		String monitorId = params.get("monitorId");// 班长（员工ID）
		// 班长配置班组
		String sql = "update t_worker set  groupid='" + groupId + "' where rec_id ='" + monitorId + "'";

		a1 = dao.update(sql);

		if (a1 > 0) {
			// 设置用户类型（1.普通工人 2.班长）
			sql = "update t_worker set  user_type='2' where rec_id ='" + monitorId + "'";
			a2 = dao.update(sql);
		}
	}
	
	
	public static void setWorker(HashMap<String, String> params) {
		PubDao dao = AppContext.getInstance().getPubDao();
		// 废除原来的班长为普通工人
		String sql = "UPDATE t_worker SET  user_type='1' WHERE rec_id NOT IN((SELECT monitor_id FROM t_worker_group))";
		//String sql = "UPDATE t_worker t SET  user_type='1' WHERE NOT EXISTS (SELECT * FROM t_worker_group g WHERE g.MONITOR_ID=t.rec_id) ";
		dao.update(sql);

	}

	/**
	 * 班组下拉列表
	 * 
	 * @param params
	 * @return
	 */
	public static List selectGroups(HashMap<String, String> params) {
		PubDao dao = AppContext.getInstance().getPubDao();
		String sql = " SELECT T.REC_ID,T.NAME,T.MONITOR,T.TEL,T.REC_TIME,T.REC_UPDATETIME, " + " T.REC_PERSION,T.REC_UPDATEPERSION,T.REC_STATUS " + " FROM T_WORKER_GROUP T "
				+ " WHERE T.REC_STATUS!=2";
		return dao.selectList(sql);
	}

	public static int isMonitor(HashMap<String, String> params) {
		PubDao dao = AppContext.getInstance().getPubDao();
		String groupId = params.get("recId");// 班组ID
		String monitorId = params.get("monitorId");// 班长（员工ID）
		// 班长配置班组
		String sql = "select count(rec_id) from t_worker_group t where  MONITOR_ID='" + monitorId + "'";
		return dao.selectListCot(sql);
	}

}
